#@adminLayout() #define main()
<div class="card">
	<div class="card-header">
		<span class="card-title">菜单管理</span>
		<div class="progress">
			<div class="progress-bar bg-success" role="progressbar"
				style="width: 100%" aria-valuenow="100" aria-valuemin="0"
				aria-valuemax="100"></div>
		</div>
		<small class="card-subtitle"> <a data-pjax
			class="btn btn-secondary btn--icon-text waves-effect"
			href="/admin/sys/menu/addOrEdit"><i
				class="zmdi zmdi-plus zmdi-hc-fw"></i> 新增</a>
		</small>
	</div>

	<div class="card-block">
		<div class="row">
			<div class="col-sm-6">
				<div class="accordion" id="itemAccordion">
					#for(x:taxonomyList)
					<div class="card">
						<div class="card-header">
							<div class="row">
								<div class="col-sm-6">
									<a class="collapsed card-title" data-toggle="collapse"
										data-parent="#itemAccordion" onclick="kitem_model(#(x.id))"
										href="#collapseItem#(x.id)">#if(0==x.isEnable)<span
										style="color: red">系统名称：#(x.name)【状态:停用】</span>#else<span
										style="color: #00CC33;">系统名称：#(x.name)【状态:启用】</span>#end
									</a>
								</div>
								<div class="col-sm-6">
									<a href="#">启用或停用</a>
								</div>

							</div>

						</div>
						<div id="collapseItem#(x.id)" class="collapse">
							<div class="card-block" id="modelAccordion#(x.id)"></div>
						</div>
					</div>
					#end
				</div>
			</div>
			<div class="col-sm-6">
				<form class="form-horizontal clearfix margin-top-30" id="jmisForm"
					action="/admin/sys/menu/saveOrUpdate" method="post">
					<div class="row">
						<input type="hidden" name="menu.id" id="modeId"
							value="#(model.id??)">
						<div class="col-sm-6">
							<div class="form-group form-group--select">
								<lable>系统信息</lable>
								<select class="select2" id="taxonomySelect" name="menu.taxId">
									#for(x:taxonomyList)
									<option value="#(x.id??)" #if(x.id??== model.taxId??) selected
										#end>#(x.name??)</option> #end
								</select>
							</div>
						</div>
						<div class="col-sm-6">
							<div class="form-group">
								<lable>类型</lable>
								<br /> <label class="custom-control custom-radio"> <input
									type="radio" name="menu.state1" class="custom-control-input"
									#if(model.superior??== "-1")checked#end id="modelRadio" checked
									value="0"> <span class="custom-control-indicator"></span>
									<span class="custom-control-description">模块</span>
								</label> <label class="custom-control custom-radio"> <input
									type="radio" name="menu.state1" class="custom-control-input"
									#if(model!=null&&model.superior??!=-1)checked#end
									id="menuRadio" value="1"> <span
									class="custom-control-indicator"></span> <span
									class="custom-control-description">菜单</span>
								</label>
							</div>
						</div>
						<div class="col-sm-6" id="modelDiv" style="display: none">
							<div class="form-group form-group--select">
								<lable>模块信息</lable>
								<select class="select2" id="modelSelect" name="menu.state">

								</select>
							</div>
						</div>
						<div class="col-sm-6" id="menuDiv" style="display: none">
							<div class="form-group form-group--select">
								<lable>菜单信息</lable>
								<select class="select2" id="menuSelect" name="menu.superior">

								</select>
							</div>
						</div>

						<div class="col-sm-6">
							<div class="form-group">
								<lable>名称</lable>
								<input type="text" class="form-control" name="menu.name"
									value="#(model.name??)" placeholder="请输入名称"> <i
									class="form-group__bar"></i>
							</div>
						</div>
						<div class="col-sm-6">
							<div class="form-group">
								<lable>序号</lable>
								<input type="text" class="form-control" id="menuSerial"
									name="menu.serialNumber" value="#(model.serialNumber??)"
									placeholder="请输入序号"> <i class="form-group__bar"></i>
							</div>
						</div>
						<div class="col-sm-6">
							<div class="form-group">
								<lable>地址</lable>
								<input type="text" class="form-control" name="menu.url"
									value="#(model.url??)" placeholder="请输入url地址"> <i
									class="form-group__bar"></i>
							</div>
						</div>
						<div class="col-sm-6">
							<div class="form-group">
								<lable>图标</lable>
								<input type="text" class="form-control" name="menu.icon"
									value="#(model.icon??)" placeholder="请输入图标"> <i
									class="form-group__bar"></i>
							</div>
						</div>
						<div class="col-sm-6">
							<div class="form-group">
								<lable>状态</lable>
								<select class="select2" name="menu.status">
									<option value="1" #if(model.status??==1) selected="selected" #end>启用</option>
									<option value="0" #if(model.status??==0) selected="selected" #end>禁用</option>
								</select>
							</div>
						</div>
						<div class="col-sm-12">
							<div class="form-group">
								<div class="col-sm-offset-4 col-sm-8">
									<button type="submit"
										class="btn btn-success btn--icon-text waves-effect">
										<i class="zmdi zmdi-check"></i> 提交
									</button>
								</div>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>

	</div>
</div>
<script src="/assets/app/table.js?v=#(resource_version)"></script>
<script>
    function kitem_model(id) {
        $.post("/admin/sys/menu/findModel", {"taxId": id}, function (ret) {
            var html = '<div class="accordion" id="modelAccordion">';
            $.each(ret.data, function (i, v) {
                html += '<div class="card">' +
                    '<div class="card-header"><div class="row"><div class="col-sm-6">' +
                    '<a class="collapsed card-title" data-toggle="collapse" data-parent="#modelAccordion" onclick="kmodel_menu('+id+',' + v.id + ')" href="#collapseModel' + v.id + '"><span style="color: #0b75c9;margin-left: 1%">模块名称：</span>' + v.name + '</a>' +
                    '</div><div class="col-sm-6"><a href="#">启用或停用</a></div></div></div>' +
                    '<div id="collapseModel' + v.id + '" class="collapse">' +
                    '<div class="card-block" id="menulAccordion' + v.id + '">' +
                    '</div>' +
                    '</div>' +
                    '</div>';
            });
            html += '</div>';
            $("#modelAccordion" + id).html(html);
        })
    }
    function kmodel_menu(taxId,mid) {
        $.post("/admin/sys/menu/findMenu", {"taxId": taxId,"modelId":mid}, function (ret) {
            var html = '<table class="table table-hover mb-0" style="margin-left: 2%;"><thead style="color: #0b75c9"><tr><th>菜单名称</th><th>链接地址</th><th>图标</th><th>状态</th><th>操作</th></tr></thead><tbody>';
            $.each(ret.data, function (i, v) {
                html+='<tr><td>'+v.name+'</td><td>'+v.url+'</td><td><i class="'+v.icon+'"></i></td><td>'+v.status+'</td><td>' +
                    '<a data-pjax class="btn btn-secondary btn--icon-text waves-effect"  href="/admin/sys/menu/addOrEdit?id='+mid+'" title="修改"> <i class="zmdi zmdi-edit"></i></a>' +
                    '<button class="btn btn-secondary btn--icon-text waves-effect" onclick="deleteData('+mid+')"><i class="zmdi zmdi-local-drink zmdi-hc-fw" title="删除"></i></button>' +
                    '</td></tr>';
            });
            html+='</tbody></table>';
            $("#menulAccordion" + mid).html(html);
        })
    }

    var superior;
    $(function () {
        superior = "#(model.superior ??)" || null;
        if (superior != null && superior !== "-1") {
            $("#menuDiv").css('display', '');
            $("#modelDiv").css('display', '');
            menuState();
        }
        if (superior == null) {
            getState();
        }
        /*类型模块点击事件*/
        $("#modelRadio").click(function () {
            $("#menuDiv").css('display', 'none');
            $("#modelDiv").css('display', 'none');
            getState();
            //menuSel();
        });
        /*点击菜单类型加载模块信息*/
        $("#menuRadio").click(function () {
            menuState();
        });
        /*模块信息改变事件*/
        $("#modelSelect").change(function () {
            modelMenu();
        });
        /*系统信息改变事件*/
        $("#taxonomySelect").change(function () {
            var radioVal = $("input[name='menu.state1']:checked").val();
            if ("1" == radioVal) {
                menuState();
            }
            getState();
        });

        /**/
        $('#menuSelect').change(function () {
            menuSel();
        })
        
        sdmForm("/admin/sys/menu");
    });

    function menuSel() {
        var taxId = $("#taxonomySelect").val();/*系统*/
        var menuId = $("#menuSelect").val();/*菜单*/
        var modelId = $("#modelSelect").val();/*模块*/
        $.post("/admin/sys/menu/findMaxMenuSerial", {
            "menuId": menuId,
            modelId: modelId,
            taxId: taxId
        }, function (data) {
            $("#menuSerial").val(data.maxSerial);
        })
    }

    /*类型菜单事件*/
    function menuState() {
        var opt = $("#taxonomySelect").val();
        if (null == opt) {
            return false;
        }
        $.ajax({
            type: "POST",
            url: "/admin/sys/menu/findByTaxonomyMenu",
            data: {"id": opt},
            dataType: "json",
            success: function (data) {
                $('#menuSelect').empty();
                var html = "";
                var id = $("#modeId").val();
                $.each(data.data, function (commentIndex, comment) {
                    html += "<option " + (comment['id'] == #(model.state ?? 1) ? 'selected' : '') + " value='" + comment['id'] + "'>" + comment['name'] + "</option>";
                });
                $('#modelSelect').html(html);
                $("#modelDiv").css('display', 'block');
                $("#menuDiv").css('display', 'block');
                modelMenu();
            }
        });
    }

    function modelMenu() {
        var opt = $("#modelSelect").val();
        var state = "#(model.state ??)" || null;
        if (opt == null) {
            return false;
        }
        $.post('/admin/sys/menu/findByModelMenu', {id: opt}, function (data) {
            var html = "<option value='0'" + (state != null && state == 0 ? "selected" : "") + ">主菜单</option><option " + (state != null && state == opt ? "selected" : "") + " value='" + opt + "'>非主菜单</option>";
            $.each(data, function (commentIndex, comment) {
                html += "<option " + (comment['id'] == #(model.superior ?? 1) ? 'selected' : '') + " value='" + comment['id'] + "'" + (state != null && state == comment['id'] ? "selected" : "") + ">" + comment['name'] + "</option>";
            });
            $('#menuSelect').html(html);

            if (superior == null) {
                getState();
            }
        })
    }

    function getState() {
        $("#menuSerial").val("");
        var taxId = $("#taxonomySelect").val();/*系统*/
        var state = $("input[name='menu.state1']:checked").val();
        if (state == "0") {//模块处理
            $.post("/admin/sys/menu/findMaxModelSerial", {"taxId": taxId}, function (data) {
                $("#menuSerial").val(data.maxSerial);
            })
        } else if (state == "1") {
            var modelId = $("#modelSelect").val();/*模块*/
            var menuId = $("#menuSelect").val();/*菜单*/
            if ($("#menuSelect option:selected").text() == "非主菜单") {
                taxId = null;
            }
            if ("" == modelId || menuId == "") {
                return false;
            }
            $.post("/admin/sys/menu/findMaxMenuSerial", {
                "taxId": taxId,
                "modelId": modelId,
                "menuId": menuId
            }, function (data) {
                $("#menuSerial").val(data.maxSerial);
            })

        }
    }
    
</script>
#end
